const { Controller } = require('varal');

class DictionaryController extends Controller {
	async index() {
		const { name, code, parent_id, root_id, root_code } = this.app.fields;
		const serv = this.app.service('DictionaryService');

		return serv.get({
			name,
			code,
			parent_id,
			root_id,
			root_code
		}).then((data) => {
			this.app.json({
				err_code: 0,
				data
			})
		})
	}

	async info() {
		const { id, code } = this.app.fields;
		const serv = this.app.service('DictionaryService');

		const data = await serv.info({ id, code });

		this.app.json({
			data
		})
	}

	async delete() {
		const { id } = this.app.fields;

		const serv = this.app.service('DictionaryService');

		return serv.delete(id).then(() => {
			this.app.json({
				err_code: 0
			})
		})
	}

	async save() {
		const params = this.app.fields;

		const serv = this.app.service('DictionaryService');

		return serv.save(params).then((data) => {
			this.app.json({
				err_code: 0,
				data
			})
		}).catch((err) => {
			this.app.json({
				err_code: 1,
				err_msg: err.toString()
			})
		});

	}
}

exports = module.exports = DictionaryController;
